<style scoped="scoped" lang="scss">
	@import "./versionUpdate.scss";
</style>
<template>
	<view class="content">
		<uni-popup ref="popup" type="center" :custom="true" :mask-click="true">
			<view class="uni-tip">
				<view class="uni-tip-title">版本更新</view>
				<view class="uni-tip-content" v-if="app.updateInfo">
				<textarea :value="app.updateInfo.remark" auto-height style="width: initial;" disabled></textarea>
				</view>
				<view v-if="app.startProgress" class="smallTitle">
					<text>下载进度:{{ app.downloadProgress }}%</text>
					<progress :percent="app.downloadProgress" stroke-width="4" />
				</view>
				<view class="uni-tip-group-button">
					<view class="uni-tip-button" @click="closeTask()" v-if="app.status==0">稍后再试</view>
					<view class="uni-tip-button" @click="createTask(app.updateInfo.android+'&Authorization='+userInfo.token)">确定</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	let that
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	var downloadTask=null
	export default {
		components: {
		},
		data() {
			return {
				isOnce:true,//如果是默认更新，则在下次打开APP才提下更新
				app: {
					status:0,//0默认更新，1强制更新
					version: '1.0.0', //版本号
					downloadProgress:0,//下载进度
					startProgress: false, // 下载进行 - 标记
					downloadTask:null,// 创建下载任务对象
					packgePath:null,//app安装地址
					updateInfo: null, // 更新摘要
					fileSize:0,//安装包大小
				}
			}
		},
		computed: mapGetters(['userInfo', 'api']),
		created() {
			that=this
		},
		methods: {
			//获取当前应用的版本号
			plusReady() {
				// 获取本地应用资源版本号
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					that.app.version = inf.version;
					//获取系统信息版本更新
					uni.getSystemInfo({
						success: (res) => {
							//检测当前平台，如果是安卓则启动安卓更新
							if (res.platform == "android") {
								that.AndroidCheckUpdate('android');
							} else {
								that.AndroidCheckUpdate('ios');
							}
						}
					})
				});
			},
			//版本跟新
			AndroidCheckUpdate(client) {
				let req = {
					version: that.app.version,
					client: client
				}
				this.$u.post(that.api.GLOBAL_auth_appVersion_checkVersion, req).then(res => {
					if (res.code == 200 && res.data) {
						that.app.status=res.data.status||0
						that.app.updateInfo=res.data
						that.app.fileSize=res.data.fileSize

						if (plus.os.name === 'iOS') {
							if(that.isOnce&&that.app.status==0){
								uni.showModal({
									title: '提示',
									content: '有新的版本发布，是否前去更新',
									success: function(res) {
										if (res.confirm) {
											plus.runtime.openURL(
												'itms-apps://apps.apple.com/us/app/%E4%BA%92%E8%81%94%E4%BA%91%E7%8E%BA/id1476214944?uo=4');
										}
									}
								});
								that.isOnce=false
							}else if(that.app.status==1){//强制更新
								uni.showModal({
									title: '提示',
									content: '有新的版本发布，是否前去更新',
									showCancel:false,
									success: function(res) {
										if (res.confirm) {
											plus.runtime.openURL(
												'itms-apps://apps.apple.com/us/app/%E4%BA%92%E8%81%94%E4%BA%91%E7%8E%BA/id1476214944?uo=4');
										}
									}
								});
								uni.hideTabBar()
							}
						} else {
							if(that.isOnce&&that.app.status==0){
								that.$refs.popup.open()
								that.isOnce=false
							}else if(that.app.status==1){//强制更新
								that.$refs.popup.open()
								uni.hideTabBar()
							}
						}
					}else{
						uni.showTabBar()
					}

				})
			},
			// 开始下载任务
			createTask(downloadLink) {
				this.app.downloadProgress = 0
				this.app.startProgress = true
				downloadTask = plus.downloader.createDownload(downloadLink, {}, function(d, status){
					// 下载完成
					if(status == 200){
						//console.log("Download success: " + d.filename);
						that.app.packgePath=d.filename
						that.$refs.popup.close()
						that.app.startProgress = false
						that.installPackge()
					} else {
						 //console.log("Download failed: " + status);
					}
				});
				downloadTask.addEventListener("statechanged", onStateChanged, false);//监听下载状态
				downloadTask.start();
				function onStateChanged(download, status){
					that.app.downloadProgress =Math.floor((download.downloadedSize/that.app.fileSize).toFixed(2)*100)
				}
			},
			// 关闭下载任务
			closeTask() {
				that.$refs.popup.close()
				try{
					downloadTask.abort()
					downloadTask = null
				}catch(e){
					//TODO handle the exception
				}
				this.app.startProgress = false
			},
			// 进行安装
			installPackge() {
				// 安装更新
				plus.runtime.install(this.app.packgePath, {
					force: true
				})
			},


		}
	}
</script>
